#include "qwsliststations.h"
#include <iostream>

QWSListStations::QWSListStations(QSettings *appSettings, QWidget *parent) :
    QWSAbstractWidget(parent),
    settings(appSettings)
{
	createTableList();
}

void QWSListStations::createTableList() {
	tableView = new QTableView();

	// Add child widget to the Composite Widget layout.
	customLayout->addWidget(tableView);
    customLayout->setAlignment(tableView, Qt::AlignRight);

    settings->sync();

    tableModel = new QStandardItemModel();
    tableModel->setHorizontalHeaderLabels(QStringList() << tr("Radio"));
    tableView->setModel(tableModel);
    tableView->setAlternatingRowColors(true);
    tableView->resizeColumnToContents(0);
    tableView->sortByColumn(0, Qt::AscendingOrder);
    tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
	tableView->setShowGrid(false);


    refresh();
}

void QWSListStations::refresh() {
    settings->sync();
    unsigned int arraySize;
    arraySize = settings->beginReadArray(QLatin1String("Radio Stations"));

    if (0 == arraySize) {
        settings->endArray();
        return;
    }

    static unsigned int i, tableSize;
    for (i = 0, tableSize = 0; i< arraySize; ++i) {
        settings->setArrayIndex(i);

        static QString radio_name = settings->value(QLatin1String("radio_name")).toString();
        if (radio_name.isEmpty()) {
            return;
        }

        std::cout << radio_name.toStdString() << std::endl;
        QStandardItem *item = new QStandardItem(radio_name);
        tableModel->setItem(tableSize, item);

        ++tableSize;
    }

    settings->endArray();

    tableView->resizeColumnsToContents();
}

void QWSListStations::refreshStations() {
    refresh();
}

QWSListStations::~QWSListStations()
{

}
